<template>
  <div class="dialog">
    <el-dialog
      :title="'设备模板' + titleText"
      :visible.sync="showDialog"
      width="55%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="型号名称：" class="w50p" prop="modelName">
          <el-input
            v-model="formData.modelName"
            clearable
            placeholder="请输入型号名称"
          />
        </el-form-item>
        <el-form-item label="关联设备类型：" class="w50p" prop="equipmentTypeCode">
          <el-cascader
            v-model="formData.equipmentTypeCode"
            :props="defaultProps"
            :options="equipmentTypeList"
            :show-all-levels="false"
            clearable
          />
        </el-form-item>
        <el-form-item prop class="w100p">
          <el-table
            ref="rightTableData"
            :data="rightTableData"
            border
            stripe
            style="position: relative"
          >
            <el-table-column label="参数名称" prop="paramName">
              <template slot-scope="scope">
                <el-input v-model="scope.row.paramName" placeholder="请选输入参数名称" />
              </template>
            </el-table-column>
            <el-table-column label="参数字段" prop="paramValue">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.paramValue"
                  placeholder="请选输入参数字段"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="80">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  style="color: red; text-decoration: underline"
                  size="small"
                  @click.native.prevent="
                    deleteRow(scope.$index, rightTableData)
                  "
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item class="w100p">
          <el-button
            type="info"
            plain
            icon="el-icon-circle-plus-outline"
            class="addInfo"
            @click="addRow"
          >添加</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import equipTemplateApi from "@/api/processing-management/equip-manage/equip-template";
import equipModelApi from "@/api/processing-management/equip-manage/equip-model";
import equipTypeApi from "@/api/processing-management/equip-manage/equip-type/index";

export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        modelName: [
          {
            required: true,
            message: "请输入型号名称",
            trigger: ["change", "blur"]
          }
        ],
        equipmentTypeCode: [
          {
            required: true,
            message: "请选择关联设备类型",
            trigger: ["change", "blur"]
          }
        ]
      },
      defaultProps: {
        children: "childList",
        value: "equipmentTypeCode",
        label: "equipmentTypeName",
        emitPath: false,
        checkStrictly: true
      },
      // 关联设备类型
      equipmentTypeList: [],
      showDialog: this.show,
      rightTableData: []
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      if (v === true) {
        this.queryEquipmentType();
      }
    },
    nowChanceStau(v) {
      if (v === "add") {
        this.formData = {};
        this.rightTableData = [];
      }
      if (v === "edit") {
        this.formData = this.editObject;
        this.rightTableData = this.editObject.equModelTemplateList;
      }
    }
  },
  created() {
    this.queryEquipmentType();
  },
  mounted() {},
  methods: {
    // 关联设备类型
    queryEquipmentType() {
      this.loading = false;
      // 获取数据
      let param = {};
      equipTypeApi.queryTree(param).then(res => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || [];
          this.equipmentTypeList = data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 增加行
    addRow() {
      var list = {
        paramName: "",
        paramValue: ""
      };
      this.rightTableData.unshift(list);
    },
    // 删除改行
    deleteRow(index, rows) {
      if (rows[index].templateCode) {
        this.del(rows, index);
      } else {
        rows.splice(index, 1);
      }
    },
    del(data, index) {
      var form = new FormData();
      form.append("templateCodeList", data[index].templateCode);
      equipTemplateApi.delList(form).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          data.splice(index, 1);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            this.formData.equModelCode = this.editObject.equModelCode;
            this.formData.organizationMark = sessionStorage.organizationNo;
            equipModelApi.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.saveTempalte();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else if (this.nowChanceStau === "add") {
            this.formData.organizationMark = sessionStorage.organizationNo;
            equipModelApi.add(this.formData).then(res => {
              if (res.code === 1) {
                var data = res.data || {};
                this.formData.equModelCode = data;
                this.$message.success(res.msg);
                this.saveTempalte();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          return false;
        }
        this.showDialog = false;
      });
    },
    saveTempalte() {
      var addparams = [];
      var editparams = [];
      var addstate = "";
      var editstate = "";
      var data = 0;
      this.rightTableData.forEach(a => {
        if (a.templateCode) {
          editstate = "edit";
          editparams.push(a);
        } else {
          addstate = "add";
          addparams.push(a);
        }
        if (a.paramName) {
          data = data + 1;
        }
      });
      if (data !== 0) {
        if (editstate === "edit") {
          var params1 = {
            equModelCode: this.formData.equModelCode,
            params: editparams
          };
          equipTemplateApi.edit(params1).then(res => {
            if (res.code === 1) {
              this.$message.success("修改成功！");
              this.close();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
        if (addstate === "add") {
          var params2 = {
            equModelCode: this.formData.equModelCode,
            params: addparams
          };
          equipTemplateApi.add(params2).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.close();
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      }
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
.el-table {
  max-height: 50vh;
  overflow-y: auto;
  overflow-x: hidden;
}
::v-deep .el-table th {
  color: #000;
  background-color: rgba(120, 182, 193, 0.4) !important;
}
::v-deep .el-table .el-table__row--striped {
  color: #000;
  background-color: rgba(186, 186, 186, 0.4) !important;
}
::v-deep.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group,
.el-table th.is-leaf {
  border-color: #d3d3d3 !important;
}
::v-deep .el-table th.is-leaf {
  border-color: #d3d3d3 !important;
}
::v-deep .el-table__empty-text {
  color: #000;
}
.addInfo {
  width: 100%;
  margin: 10px;
  background-color: #006e91;
  color: #fff;
  font-size: 18px;
}
</style>
